﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data;
using MySql.Data.MySqlClient;


namespace WindowsFormsApplication1
{
    public partial class PxAdmin : Form
    {
        private int state = 0;
        string nowdate;
        dbutils db=new dbutils();
        public PxAdmin()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            loaddata(0);
        }

        private void loaddata(int type)
        {
            state = type;
            string connection;
            string query="";
            DataSet myds;
            connection = dbutils.constr;

            if (type == 0)
            {
                query = "select staffid,staffname,applydate from px_table where state=0 order by applydate DESC";
                label1.Text = "正在查看所有已报名参加培训的人员：";
                label2.Text = "输入人员编号安排他/她参加培训：";
                textBox2.Enabled = false;
                textBox1.Enabled = true;
                button4.Enabled = true;
            }
            if (type == 1)
            {
                query = "select staffid,staffname,applydate,startdate from px_table where state=1 order by applydate DESC";
                label1.Text = "正在查看所有正在参加培训的人员：";
                label2.Text = "输入人员编号及其成绩安排他/她结束培训：";
                textBox2.Enabled = true;
                textBox1.Enabled = true;
                button4.Enabled = true;
            }
            if (type == 2)
            {
                query = "select px_table.staffid,px_table.staffname,px_table.applydate,px_table.startdate,px_table.enddate,px_cj_table.grade  from px_table left join px_cj_table on px_table.staffid=px_cj_table.staffid where px_table.state=2 order by px_table.applydate DESC";
                label1.Text = "正在查看所有培训结束的人员：";
                label2.Text = "";
                textBox1.Enabled = false;
                textBox2.Enabled = false;
                button4.Enabled = false;
            }
            MySqlConnection conn = new MySqlConnection(connection);
            MySqlDataAdapter adapter = new MySqlDataAdapter();
            conn.Open();
            adapter.SelectCommand = new MySqlCommand(query, conn);
            myds = new DataSet();
            adapter.Fill(myds, "Bill");
            bindingSource1.DataSource = myds.Tables["Bill"];
            dataGridView1.DataSource = bindingSource1;
            
            dataGridView1.Columns["staffid"].HeaderText = "员工编号";
            dataGridView1.Columns["staffname"].HeaderText = "员工姓名";
            dataGridView1.Columns["applydate"].HeaderText = "申请日期";
            if(type>=1)
                dataGridView1.Columns["startdate"].HeaderText = "开始日期";
            if (type == 2)
            {
                dataGridView1.Columns["enddate"].HeaderText = "结束日期";
                dataGridView1.Columns["grade"].HeaderText = "分数";
            }
         
            conn.Close();

        }

        private void PxAdmin_Load(object sender, EventArgs e)
        {
            loaddata(0);
            nowdate = DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString();
            
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string sql3=
                "select * from staff where loginname='"+textBox1.Text+"'";
            string name=db.loginsql(sql3);
            if(name==null){
                MessageBox.Show("查无此人!");
                return;
            }

            if(state==0)
            {
                string sql1 =
                    "update px_table set startdate='"+nowdate+"',state=1 where staffid='"+textBox1.Text+"' and state=0";
                if(MessageBox.Show("确定安排 " + name + " 参加培训吗？", "提示", MessageBoxButtons.OKCancel) == DialogResult.Cancel){
                    return;
                }
                if(db.insert(sql1)>0){
                    MessageBox.Show("安排成功！");
                    loaddata(0);
                    return;
                }
                MessageBox.Show("安排失败！");
            }
            if(state==1){ 
                if(MessageBox.Show("确定安排 " + name + " 结束培训吗？", "提示", MessageBoxButtons.OKCancel) == DialogResult.Cancel){
                    return;
                }
                string sql2 =
                    "update px_table set enddate='" + nowdate + "',state=2 where staffid='" + textBox1.Text + "' and state=1";
                string sql4 =
                    "insert px_cj_table values('" + textBox1.Text + "','" + name + "'," + textBox2.Text + ",'" + nowdate + "')";
                if (db.insert(sql2) == 0 || db.insert(sql4) == 0)
                {
                    MessageBox.Show("error!");
                    return;
                }
                MessageBox.Show("安排成功！");
                loaddata(1);

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            loaddata(1);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            loaddata(2);
        }


    }
}
